package com.aeon.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

public class StackLayoutComposit extends Composite {

	public StackLayoutComposit(Composite parent, int style) {
		super(parent, style);
		init();

	}

	private void init() {

		setLayout(new RowLayout(SWT.VERTICAL));
		final Composite mUpper = new Composite(this, SWT.NONE);
		final StackLayout layout = new StackLayout();
		mUpper.setLayout(layout);
		final Group group1 = createGroup(mUpper, "Group1", 4);
		final Group group2 = createGroup(mUpper, "Group2", 6);
		layout.topControl = group1;
		Button next = new Button(this, SWT.PUSH);
		next.setText("Show next");
		next.pack();
		next.addMouseListener(new MouseListener() {

			@Override
			public void mouseUp(MouseEvent e) {
				if (layout.topControl == group2) {
					layout.topControl = group1;
				}else{
					layout.topControl = group2;
				}
				mUpper.layout();
			}

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub

			}
		});

	}

	private Group createGroup(Composite mUpper, String string, int m) {
		Group group = new Group(mUpper, SWT.NONE);
		group.setText(string);
		group.setLayout(new GridLayout(m / 3, false));

		for (int i = 0; i < m; i++) {
			Button button = new Button(group, SWT.PUSH);
			button.setText(string + " " + i);
		}
		return group;
	}

}
